﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Services;
using GWS.Entities;
using GWS.DAL;

namespace GWS
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public bool UpdatePatient(Patient patient)
        {
            return PatientAccess.UpdatePatient(patient);
        }

        [WebMethod]
        public int ImportPatient(DataTable importList)
        {
            return PatientAccess.ImportPeople(ConvertDataTable(importList));
        }

        [WebMethod]
        public bool SavePatient(Patient pat)
        {
            return PatientAccess.AddPatient(pat);
        }

        [WebMethod]
        public Patient GetPatient(string UserID)
        {
            return PatientAccess.GetPatient(UserID);
        }

        [WebMethod]
        public Patient GetPatientByID(string PatientID)
        {
            return PatientAccess.GetPatientByID(PatientID);
        }

        [WebMethod]
        public Patient[] GetAllPatients()
        {
            return PatientAccess.GetAllPatients();
        }

        [WebMethod]
        public bool SaveAppt(Appointment appt)
        {
            return AppointmentAccess.AddAppointment(appt);
        }

        [WebMethod]
        public Appointment GetAppt()
        {
            return new Appointment();
        }

        #region Private Methods
        /// <summary>
        /// Converts a DataTable object into a Person array.
        /// </summary>
        /// <param name="table">DataTable object that has the imported data.</param>
        /// <returns></returns>
        private Patient[] ConvertDataTable(DataTable table)
        {
            Patient[] people = new Patient[table.Rows.Count];

            //counters
            int x = 0;
            int y = 0;

            //creates all the person objects
            while (y < table.Rows.Count)
            {
                people[y] = new Patient();
                y++;
            }

            //Goes through each row PER COLUMN.
            foreach (DataColumn col in table.Columns)
            {
                x = 0;
                while (x < table.Rows.Count)
                {
                    foreach (DataRow roz in table.Rows)
                    {
                        if (col.ColumnName == "LastName")
                            people[x].LastName = roz[col].ToString();
                        else if (col.ColumnName == "FirstName")
                            people[x].FirstName = roz[col].ToString();
                        else if (col.ColumnName == "Address")
                            people[x].Address = roz[col].ToString();
                        else if (col.ColumnName == "Zip")
                            people[x].Zip = roz[col].ToString();
                        else if (col.ColumnName == "State")
                            people[x].State = roz[col].ToString();
                        else if (col.ColumnName == "City")
                            people[x].City = roz[col].ToString();
                        else if (col.ColumnName == "Phone")
                            people[x].Phone = roz[col].ToString();
                        else if (col.ColumnName == "Healthcare")
                            people[x].Healthcare = Convert.ToBoolean(roz[col].ToString());
                        else if (col.ColumnName == "Diagnosis")
                            people[x].Diagnosis = roz[col].ToString();
                        x++;
                    }
                }
            }
            return people;
        }
        #endregion
    }
}
